
const innerAudioContext = wx.createInnerAudioContext({
  useWebAudioImplement: false
})

Page({
  data: {
    artist: {},
    songs: [],
    currentMp3Id: null,
  },
  onLoad(options) {
    // 只能在页面 onLoad 中接受参数
    // console.log(options)
    const self = this
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      method: 'GET',
      url: `https://music.qier222.com/api/artists?id=${options.aid}`,
      success(res) {
        console.log(res.data)
        self.setData({
          artist: res.data.artist,
          songs: res.data.hotSongs
        })
      },
      complete() {
        wx.hideLoading()
      }
    })
  },
  playMusic(event) {
    const { mid } = event.target.dataset

    console.log(`https://music.163.com/song/media/outer/url?id=${mid}.mp3`)
    innerAudioContext.src = `https://music.163.com/song/media/outer/url?id=${mid}.mp3`

    if (mid === this.data.currentMp3Id) {
      // 暂停
      innerAudioContext.pause()
      this.setData({
        currentMp3Id: null
      })
    } else {
      innerAudioContext.play() // 播放
      console.log(innerAudioContext)
      this.setData({
        currentMp3Id: mid
      })
    }
  }
})